$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_ydczjhwcb_m")
    const dgrid1 = $NG.getCmpApi("p_form_ydczjhwcb_d1")
    const getHeadValue = (str) => mstform.getItem(str).getValue()


    if (page.oType == 'add') {
        getToMonth()
        getOneMonth()
        mstform.getItem("u_nd").setValue({
            label: new Date().getFullYear(),
            value: new Date().getFullYear()
        })
    }

    useValuesChange(async () => {
        await getOneMonth()
        getToMonth()
    }, "p_form_ydczjhwcb_m.u_htmc")

    useValuesChange(async () => {
        await getOneMonth()
        getToMonth()
    }, "p_form_ydczjhwcb_m.u_yd")

    useUpdateRow(({ args }) => {
        console.log(`args`, args[0]);
        const item = args[0]
        if(getHeadValue("u_yd") != '6870000000000553'&&(Number(item.u_njh) || 0) != window._cath_one_month_njh){
            $NG.message("一月份外，年计划不得修改!")
            item.u_njh = window._cath_one_month_njh
        }
        dgrid1.refreshView()
    })
    useBeforeClick(() => {
        const u_yd = mstform.getItem("u_yd").getValue()
        if (!u_yd) {
            updateRowslock = true
            $NG.alert("请先填写表头月度字段！")
            return false
        }
    }, "addrow")



    // 拉取上个月的金额
    async function getToMonth() {
        let phid = mstform.getItem("u_yd").getValue()
        let cnt = mstform.getItem("u_htmc").getValue()
        if (!phid || !cnt) return
        phid = (Number(phid) || 0)
        if (phid != 6870000000000553) {
            phid--
        } else {
            dgrid1.clearRows()
            return
        }
        await $NG.execServer("SELECT", { sql: `select d.u_xyczjh from p_form_ydczjhwcb_m m left join p_form_ydczjhwcb_d1 d on d.pphid= m.phid where m.u_yd = '${phid}' and m.u_htmc = '${cnt}'` }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            dgrid1.clearRows().then(() => {
                dgrid1.addRows({
                    u_yjh: datas.u_xyczjh,
                    u_njh:window._cath_one_month_njh
                })
            })
        })
    }

    // 拉取一月的年计划
    async function getOneMonth() {
        let phid = 6870000000000553
        let cnt = mstform.getItem("u_htmc").getValue()
        if (!phid || !cnt) return
        await $NG.execServer("SELECT", { sql: `select d.u_njh from p_form_ydczjhwcb_m m left join p_form_ydczjhwcb_d1 d on d.pphid= m.phid where m.u_yd = '${phid}' and m.u_htmc = '${cnt}'` }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            dgrid1.clearRows().then(() => {
                window._cath_one_month_njh = datas.u_njh
            })
        })
    }

})